﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace FindInIMDB
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            if(args.Length == 0)
            {
                string path = Application.ExecutablePath;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                return;
            }

            if (args.Length <= 1)
            {
                MessageBox.Show("wrong command line arguments");
                return;
            }
            if (!args[0].Equals("imdb", StringComparison.OrdinalIgnoreCase))
                return;

            String filename = args[1].Substring(args[1].LastIndexOf('\\') + 1);
            string[] parts = filename.Split(new char[] { '_', '.' });

            StringBuilder sb = new StringBuilder("http://www.imdb.com/find?q=");
            
            Regex rex = new Regex("[a-z0-9A-Z_]+");


            for(int i = 0;i<parts.Length; i++)
            {
                Match ma = rex.Match(parts[i]);
                if (!ma.Success)
                    break;
                sb.Append(parts[i]);
                sb.Append("+");
            }

            if (sb[sb.Length - 1] == '+')
                sb.Remove(sb.Length - 1, 1);

            System.Diagnostics.Process.Start(sb.ToString());
        }
    }
}
